package com.ygsoft.tianlu_mall.service.impl;

import com.ygsoft.tianlu_mall.mapper.GoodsMapper;
import com.ygsoft.tianlu_mall.pojo.*;
import com.ygsoft.tianlu_mall.service.GoodsService;
import com.ygsoft.tianlu_mall.service.NumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    NumService numService;


    @Override
    public List<Map<String, Object>> getToalTypeName() {
        return goodsMapper.getToalTypeName();
    }

    @Override
    public List<Map<String, Object>> getAllPropertyByTypeId(int type_id) {
        return goodsMapper.getAllPropertyByTypeId(type_id);
    }

    @Override
    public List<Map<String, Object>> getPropertyValueById(int goods_id) {
        return goodsMapper.getPropertyValueById(goods_id);
    }

    @Override
    public List<Map<String, Object>> getGoodsDetailByGoodsId(int goods_id) {
        return goodsMapper.getGoodsDetailByGoodsId(goods_id);
    }

    @Override
    public HashMap<String, Object> checkIsCollect(Integer user_id, int goods_id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("goods_id",goods_id);
        return goodsMapper.checkIsCollect(map);
    }

    @Override
    public List<Map<String, Object>> getGoodsIndex() {
        return goodsMapper.getGoodsIndex();
    }

    @Override
    public List getGoodsIdByTypeId(int type_id) {
        return goodsMapper.getGoodsIdByTypeId(type_id);
    }

    @Override
    public List<Map<String, Object>> getGoodsByTypeId(int type_id) {
        return goodsMapper.getGoodsByTypeId(type_id);
    }

    @Override
    public List<Integer> getGoodsId() {
        return goodsMapper.getGoodsId();
    }

    @Override
    public Map<String,Object> getRecommendGoodsId(int type_id) {
        return goodsMapper.getRecommendGoodsId(type_id);
    }

    @Override
    public HashMap<String, Object> getRecommendGoodsByType(int goods_id) {
        return goodsMapper.getRecommendGoodsByType(goods_id);
    }

    @Override
    public int getRecommendGoodsId2(int type_id) {
        return goodsMapper.getRecommendGoodsId2(type_id);
    }

    @Override
    public List getTypeIdList() {
        return goodsMapper.getTypeIdList();
    }

    @Override
    public List<HashMap<String,Object>> recommendGoodsInfo() {
        List<HashMap<String,Object>> type_idList = goodsMapper.getTypeIdList();
        List<HashMap<String,Object>> result = new ArrayList<>();
        for (HashMap<String,Object> type_id:type_idList) {
            //指定分类下并未有订单
            //按销量选出推荐商品
            Integer goods_id = (Integer) goodsMapper.getRecommendGoodsId((int)type_id.get("type_id")).get("goods_id");
            if (!goods_id.equals(null)){
                HashMap<String,Object> goodsInfo = goodsMapper.getRecommendGoodsByType(goods_id);
                goodsInfo.put("goodsNum",numService.getThreeNum((int)goods_id));
                result.add(goodsInfo);
            }else {
                //指定分类上架的第一件商品为推荐商品
                Integer goods_id2 = goodsMapper.getRecommendGoodsId2((int)type_id.get("type_id"));
                HashMap<String,Object> goodsInfo = goodsMapper.getRecommendGoodsByType(goods_id2);
                goodsInfo.put("goodsNum",numService.getThreeNum((int)goods_id2));
                result.add(goodsInfo);
            }
        }
        if (result!=null){
            return result;
        }else {
            return new ArrayList<>();
        }

    }

    @Override
    public List<HashMap<String, Object>> recommendGoodsInfoByTypeId(int type_id) {
        List<HashMap<String,Object>> result = new ArrayList<>();
        //指定分类下并未有订单
        //按销量选出推荐商品
        Integer goods_id = (Integer) goodsMapper.getRecommendGoodsId(type_id).get("goods_id");
        if (!goods_id.equals(null)){
            HashMap<String,Object> goodsInfo = goodsMapper.getRecommendGoodsByType(goods_id);
            goodsInfo.put("goodsNum",numService.getThreeNum((int)goods_id));
            result.add(goodsInfo);
        }else {
            //指定分类上架的第一件商品为推荐商品
            Integer goods_id2 = goodsMapper.getRecommendGoodsId2(type_id);
            HashMap<String,Object> goodsInfo = goodsMapper.getRecommendGoodsByType(goods_id2);
            goodsInfo.put("goodsNum",numService.getThreeNum((int)goods_id2));
            result.add(goodsInfo);
        }
        return result;
    }
}
